package com.eduorder.service.impl;

import com.eduorder.client.EduClient;
import com.eduorder.client.UcenterClient;
import com.eduorder.entity.Order;
import com.eduorder.mapper.OrderMapper;
import com.eduorder.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eduorder.utils.OrderNoUtil;
import com.ordervo.CourseWebVoOrder;
import com.ordervo.UcenterMemberOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-07-20
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

	@Autowired
	private UcenterClient ucenterClient;

	@Autowired
	private EduClient eduClient;

	//1 生成订单的方法
	@Override
	public String createOrder(String courseId, String memberId) {
		//通过远程调用，根据用户id获取用户信息
		UcenterMemberOrder userInfoOrder = ucenterClient.getUserInfoOrder(memberId);

		//通过远程调用，根据用户id获取用户信息
		CourseWebVoOrder courseInfoOrder = eduClient.getCourseInfoOrder(courseId);

		Order order = new Order();
		order.setOrderNo(OrderNoUtil.getOrderNo());

		order.setCourseId(courseId);
		order.setCourseTitle(courseInfoOrder.getTitle());
		order.setCourseCover(courseInfoOrder.getCover());
		order.setTeacherName(courseInfoOrder.getTeacherName());
		order.setTotalFee(courseInfoOrder.getPrice());
		order.setMemberId(memberId);
		order.setMobile(userInfoOrder.getMobile());
		order.setNickname(userInfoOrder.getNickname());

		order.setStatus(0);
		order.setPayType(1);

		baseMapper.insert(order);

		return order.getOrderNo();
	}
}
